package com.bing.utils;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by kaibb on 16-1-3.
 */
public class UtilFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(UtilFactory.class);

    public static final Gson GSON = new Gson();

    public static final Random RANDOM = new Random();

    public static final SimpleDateFormat DATE_TIME = new SimpleDateFormat("yyyy-MM-dd_hh_mm");

    private static final String $ = "@@@@$@@@@";
    private static final String EXCERPT_TOKEN_HTML = "<em>" + $ + "</em>";
        private static final String EXCERPT_TOKEN_MD = "\\*@@@@\\$@@@@\\*";

    public static void createDir(String editorDir) {
        File file = new File(editorDir);
        if (!file.exists()) {
            file.mkdir();
            file.setWritable(true);
            LOGGER.info("创建目录:{}", file.getAbsolutePath());
        }
    }

    public static String getTimeStr() {
        return DATE_TIME.format(new Date());
    }

    public static String getExcerpt(String content) {
        int endIndex = content.indexOf(EXCERPT_TOKEN_HTML);
        if (endIndex != -1) {
            return content.substring(0, endIndex);

        }
        return content;
    }

    public static String getMD(String content) {
        return content == null ? null : content.replaceFirst(EXCERPT_TOKEN_MD, "");
    }
}
